local LB_U = require "packages.lb_kill_utility.utility"

local skel = fk.CreateSkill {
  name = "lb_zero__liaosuo",
  max_branches_use_time = function(self, player)
    local list = {}
    local room = Fk:currentRoom()
    for _, to in ipairs(room.players) do
      list[tostring(to.id)] = { [Player.HistoryTurn] = 1 }
    end
    return list
  end,
}

Fk:loadTranslationTable {
  ["lb_zero__liaosuo"] = "燎索",
  [":lb_zero__liaosuo"] = "若本回合因本技能使用过牌数不大于四，你可以将一张牌当【火攻】对本回合未因此指定过的目标使用，若之未对目标造成伤害，其可将一张牌当【铁索连环】使用，否则你与其将手牌依次调整至四张，然后本回合你视为未使用过牌。",

  ["#lb_zero__liaosuo"] = "燎索：你可以将一张牌当【火攻】对本回合未因此指定过的目标使用",
  ["#lb_zero__liaosuo-use"] = "燎索：你可以将一张牌当【铁索连环】使用",

  ["@[skill_players]lb_zero__liaosuo"] = "燎索",
  ["@lb_zero__liaosuo-turn"] = "燎索",

  ["$lb_zero__liaosuo1"] = "守护，就是彻底清除威胁。",
  ["$lb_zero__liaosuo2"] = "我，就是你的终点线。",
  ["$lb_zero__liaosuo3"] = "这道防线，是不可逾越的界限。",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "fire_attack",
  prompt = "#lb_zero__liaosuo",
  filter_pattern = {
    min_num = 1,
    max_num = 1,
    pattern = ".",
  },
  view_as = function(self, player, cards)
    if #cards ~= 1 then return nil end
    local card = Fk:cloneCard("fire_attack")
    card:addSubcards(cards)
    card.skillName = skel.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    room:addPlayerMark(player, "@lb_zero__liaosuo-turn")
    room:addPlayerMark(player, "lb_zero__liaosuo-turn")
    for _, to in ipairs(use.tos) do
      player:addSkillBranchUseHistory(skel.name, tostring(to.id))
    end
  end,
  after_use = function(self, player, use)
    local room = player.room
    for _, to in ipairs(room:getAlivePlayers()) do
      if player:isAlive() and table.contains(use.tos, to) and to:isAlive() then
        if use.damageDealt and use.damageDealt[to] then
          LB_U.BalanceCards(player, skel.name, 4)
          if player ~= to then
            LB_U.BalanceCards(to, skel.name, 4)
          end
        else
          local chain = room:askToUseVirtualCard(to, {
            name = "iron_chain",
            skill_name = skel.name,
            cancelable = true,
            card_filter = {
              n = 1
            },
            prompt = "#lb_zero__liaosuo-use",
          })
          if chain then
            room:addPlayerMark(player, "@lb_zero__liaosuo-turn")
          end
        end
      end
    end
    room:removePlayerMark(player, "@lb_zero__liaosuo-turn", player:getMark("lb_zero__liaosuo-turn"))
    room:setPlayerMark(player, "lb_zero__liaosuo-turn", 0)
    player:setCardUseHistory("", 0, Player.HistoryTurn)
    player:setCardUseHistory("", 0, Player.HistoryPhase)
  end,
  enabled_at_play = function(self, player)
    return player:getMark("@lb_zero__liaosuo-turn") <= 4
  end,
  enabled_at_response = function(self, player, response)
    return not response and player:getMark("@lb_zero__liaosuo-turn") <= 4
  end,
})

skel:addEffect("prohibit", {
  is_prohibited = function(self, from, to, card)
    if from and table.contains(card.skillNames, skel.name) and card.name == "fire_attack" then
      return not skel:withinBranchTimesLimit(from, tostring(to.id), Player.HistoryTurn)
    end
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:setPlayerMark(player, "@[skill_players]lb_zero__liaosuo",
    { name = skel.name, scope = Player.HistoryTurn, players = table.map(player.room.players, Util.IdMapper) })
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[skill_players]lb_zero__liaosuo", 0)
end)

return skel
